//
//  DMLifecycleManager.h
//  DualMediationSDK
//
//  Created by DualMediation on 2024/01/01.
//  Copyright © 2024 DualMediation. All rights reserved.
//

#import <Foundation/Foundation.h>
#import <UIKit/UIKit.h>

// 导入新的组件
#import "DMLifecycle/Types/DMLifecycleTypes.h"
#import "DMLifecycle/Types/DMLifecycleProtocols.h"
#import "DMLifecycle/Core/DMLifecycleStateManager.h"
#import "DMLifecycle/Callback/DMLifecycleCallbackManager.h"
#import "DMLifecycle/Callback/DMFacebookCallbackForwarder.h"
#import "IMFCMPushModels.h"

@class UIViewController;
@class DMLifecycleEventDispatcher;

NS_ASSUME_NONNULL_BEGIN
@class DMLifecycleDelegateRegistry;
@class DMLifecycleManager;
@class DMLoginResponse;
@class DMUnAckOrderBean;
@class DMGameStrategy;
@class DMProductInfo;
@class DMPurchaseInfo;
@class DMFaceBookInfo;

#pragma mark - 生命周期管理器

/**
 * DualMediationSDK生命周期管理器
 * 重构后的主类，整合了各个组件，提供统一的生命周期管理接口
 */
@interface DMLifecycleManager : NSObject

#pragma mark - 单例访问

/**
 * 获取生命周期管理器单例
 */
+ (instancetype)sharedManager;

#pragma mark - 核心组件访问

/**
 * 代理注册中心
 */
@property (nonatomic, strong, readonly) DMLifecycleDelegateRegistry *delegateRegistry;

/**
 * 状态管理器
 */
@property (nonatomic, strong, readonly) DMLifecycleStateManager *stateManager;

/**
 * 事件分发器
 */
@property (nonatomic, strong, readonly) DMLifecycleEventDispatcher *eventDispatcher;

/**
 * SDK回调管理器
 */
@property (nonatomic, strong, readonly) DMLifecycleCallbackManager *callbackManager;

/**
 * Facebook回调转发器
 */
@property (nonatomic, strong, readonly) DMFacebookCallbackForwarder *facebookForwarder;

/**
 * SDK生命周期代理
 */
@property (nonatomic, weak, nullable) id<DMSDKLifecycleDelegate> delegate;

#pragma mark - 便捷属性访问

/**
 * 当前生命周期状态
 */
@property (nonatomic, assign, readonly) DMLifecycleState currentLifecycleState;

/**
 * 上一个生命周期状态
 */
@property (nonatomic, assign, readonly) DMLifecycleState previousLifecycleState;

/**
 * 检查SDK是否已初始化
 */
@property (nonatomic, readonly) BOOL isSDKInitialized;

/**
 * 获取当前视图控制器
 */
@property (nonatomic, assign, readonly, nullable) UIViewController *currentViewController;

#pragma mark - 初始化和配置

/**
 * 注册SDK监听器并初始化
 * @param viewController 当前视图控制器
 * @param delegate SDK回调代理
 */
- (void)registerSDKListener:(UIViewController *)viewController delegate:(id<DMSDKLifecycleDelegate>)delegate;

/**
 * 初始化Firebase消息服务
 * @param viewController 当前视图控制器
 */
- (void)initFirebaseMessage:(UIViewController *)viewController;

#pragma mark - Firebase管理方法

/**
 * 初始化Firebase Core（如果需要）
 */
- (void)initializeFirebaseIfNeeded;

/**
 * 检查FCM是否已初始化
 * @return YES表示已初始化，NO表示未初始化
 */
- (BOOL)isFCMInitialized;

/**
 * 初始化Firebase消息推送（基础版本）
 * @param completion 完成回调
 */
- (void)initializeFirebaseMessagingWithCompletion:(nullable IMFCMPushCompletionBlock)completion;

/**
 * 初始化Firebase消息推送（带目标页面）
 * @param targetViewControllerClass 点击通知时打开的页面类
 * @param completion 完成回调
 */
- (void)initializeFirebaseMessagingWithTargetViewController:(Class)targetViewControllerClass 
                                                 completion:(nullable IMFCMPushCompletionBlock)completion;

/**
 * 获取当前FCM令牌
 * @return FCM令牌，未初始化返回nil
 */
- (nullable NSString *)getCurrentFCMToken;

/**
 * 异步获取FCM令牌
 * @param completion 完成回调
 */
- (void)getFCMTokenWithCompletion:(IMFCMPushCompletionBlock)completion;

/**
 * 请求通知权限
 * @param completion 完成回调
 */
- (void)requestNotificationPermissionWithCompletion:(void(^)(BOOL granted, NSError * _Nullable error))completion;

/**
 * 处理Firebase推送通知点击
 * @param userInfo 通知用户信息
 */
- (void)handleNotificationTap:(NSDictionary *)userInfo;

/**
 * 处理Firebase推送通知点击（带视图控制器）
 * @param userInfo 通知用户信息
 * @param viewController 目标视图控制器
 */
- (void)handleNotificationTap:(NSDictionary *)userInfo viewController:(UIViewController *)viewController;

/**
 * 检查是否为Firebase通知
 * @param userInfo 通知用户信息
 * @return YES表示是Firebase通知，NO表示不是
 */
- (BOOL)isFCMNotification:(NSDictionary *)userInfo;

#pragma mark - iOS标准生命周期方法

/**
 * 应用启动完成
 * @param application UIApplication实例
 * @param launchOptions 启动选项字典
 * @return 是否成功处理启动
 */
- (BOOL)applicationDidFinishLaunching:(UIApplication *)application withOptions:(nullable NSDictionary *)launchOptions;

/**
 * 应用变为活跃状态
 * @param application UIApplication实例
 */
- (void)applicationDidBecomeActive:(UIApplication *)application;

/**
 * 应用将要失去活跃状态
 * @param application UIApplication实例
 */
- (void)applicationWillResignActive:(UIApplication *)application;

/**
 * 应用进入后台
 * @param application UIApplication实例
 */
- (void)applicationDidEnterBackground:(UIApplication *)application;

/**
 * 应用将要进入前台
 * @param application UIApplication实例
 */
- (void)applicationWillEnterForeground:(UIApplication *)application;

/**
 * 应用将要终止
 * @param application UIApplication实例
 */
- (void)applicationWillTerminate:(UIApplication *)application;

/**
 * 应用收到内存警告
 * @param application UIApplication实例
 */
- (void)applicationDidReceiveMemoryWarning:(UIApplication *)application;

/**
 * 应用收到远程推送通知
 * @param application UIApplication实例
 * @param userInfo 推送通知信息
 */
- (void)applicationDidReceiveRemoteNotification:(UIApplication *)application userInfo:(NSDictionary *)userInfo;

/**
 * 应用收到本地通知
 * @param application UIApplication实例
 * @param notification 本地通知对象
 */
- (void)applicationDidReceiveLocalNotification:(UIApplication *)application notification:(UILocalNotification *)notification;

/**
 * 应用打开URL
 * @param application UIApplication实例
 * @param url 要打开的URL
 * @param options 打开选项
 * @return 是否成功处理URL
 */
- (BOOL)applicationOpenURL:(UIApplication *)application url:(NSURL *)url options:(NSDictionary<UIApplicationOpenURLOptionsKey,id> *)options;

/**
 * 应用继续用户活动
 * @param application UIApplication实例
 * @param userActivity 用户活动对象
 * @param restorationHandler 恢复处理器
 * @return 是否成功处理用户活动
 */
- (BOOL)applicationContinueUserActivity:(UIApplication *)application userActivity:(NSUserActivity *)userActivity restorationHandler:(void(^)(NSArray<id<UIUserActivityRestoring>> * _Nullable restorableObjects))restorationHandler;

#pragma mark - 便捷方法

/**
 * 添加生命周期事件监听器
 * @param listener 监听器对象
 */
- (void)addLifecycleEventListener:(id<DMLifecycleEventListener>)listener;

/**
 * 移除生命周期事件监听器
 * @param listener 监听器对象
 */
- (void)removeLifecycleEventListener:(id<DMLifecycleEventListener>)listener;

/**
 * 检查是否处于活跃状态
 * @return 是否活跃
 */
- (BOOL)isActive;

/**
 * 检查是否处于前台状态
 * @return 是否在前台
 */
- (BOOL)isForeground;

/**
 * 检查是否处于后台状态
 * @return 是否在后台
 */
- (BOOL)isBackground;

#pragma mark - 类方法便捷接口

/**
 * 便捷方法：注册SDK监听器
 * @param viewController 当前视图控制器
 * @param delegate SDK回调代理
 */
+ (void)registerSDKListenerWithViewController:(UIViewController *)viewController delegate:(id<DMSDKLifecycleDelegate>)delegate;

/**
 * 便捷方法：处理新的Intent
 * @param userInfo 用户信息
 */
+ (void)onNewIntentWithUserInfo:(NSDictionary *)userInfo;

/**
 * 便捷方法：处理Activity结果
 * @param requestCode 请求代码
 * @param resultCode 结果代码
 * @param data 数据
 */
+ (void)onActivityResultWithRequestCode:(NSInteger)requestCode resultCode:(NSInteger)resultCode data:(nullable NSDictionary *)data;

#pragma mark - Facebook回调转发方法

/**
 * 转发Facebook登录成功回调
 * @param userInfo 用户信息字典
 */
- (void)forwardFacebookLoginSuccess:(NSDictionary *)userInfo;

/**
 * 转发Facebook登录失败回调
 * @param error 登录错误
 */
- (void)forwardFacebookLoginError:(NSError *)error;

/**
 * 转发Facebook登录取消回调
 */
- (void)forwardFacebookLoginCancel;

/**
 * 转发Facebook用户信息获取成功回调
 * @param userInfo 用户信息字典
 */
- (void)forwardFacebookUserInfoSuccess:(NSDictionary *)userInfo;

/**
 * 转发Facebook用户信息获取失败回调
 * @param error 获取用户信息错误
 */
- (void)forwardFacebookUserInfoError:(NSError *)error;

/**
 * 转发Facebook分享完成回调
 * @param success 分享是否成功
 * @param message 分享消息
 */
- (void)forwardFacebookShareComplete:(BOOL)success message:(NSString *)message;

@end

NS_ASSUME_NONNULL_END
